package com.datas.service.api;

import com.datas.beans.PageResourceApiItem;
import com.datas.utils.hbn.HbnTxRuner;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by jige0727 on 2017/5/31.
 */
@Service
public class ApiPermissionService {

    public List<PageResourceApiItem> getPageresourcesByRoleCode(String roleCode) {
        return (List<PageResourceApiItem>) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                Integer roleid = (Integer) getCurrentSession().createQuery(
                        "select i.id from UmsRole i where i.role_code=:role_code")
                        .setParameter("role_code", roleCode)
                        .uniqueResult();

                return getCurrentSession().createQuery("select new com.datas.beans.PageResourceApiItem( " +
                        "r.rese_name, r.rese_uri, r.rese_type, r.sort_order, " +
                        "r.parent_rese_id, r.parent_rese_name, r.struct ) " +
                        "from UmsPageResource r " +
                        "inner join UmsRole_UmsPageResource rr on r.id = rr.pageresources_id " +
                        "where rr.roles_id =:roleid ")
                        .setParameter("roleid", roleid).list();
            }
        }.run();
    }
}
